<?php

class Db_Tables2_BSku
{
	function __construct() {
		$this->dealers_db = Zend_Registry::get("dealers_db");
	}
	
	function GetSkuInfo($id, $id_type, $select_type) //id_type 1=product_id 2=sku_id   select_type 1=直属下级 2=所有从属下级
	{
		$result = array();
		$result['data'] = array();
		$result['self'] = array();
		
		if($id && $id_type && $select_type)
		{
			if(2 == $id_type)
			{
				$select = $this->dealers_db->select();
				$select->from("b_sku", "*");
				$select->where("sku_id = ?", $id);
				$result['self'] = $this->dealers_db->fetchRow("$select");
				
				if($result['self']['product_id'])
				{
					$select = $this->dealers_db->select();
					$select->from("b_dict_product", "product_name");
					$select->where("product_id = ?", $result['self']['product_id']);
					$row = $this->dealers_db->fetchRow($select);
					$result['self']['parent_name'] = $row['product_name'];
				}else{
					$select = $this->dealers_db->select();
					$select->from("b_sku", "sku_name");
					$select->where("sku_id = ?", $result['self']['parent_sku_id']);
					$row = $this->dealers_db->fetchRow($select);
					$result['self']['parent_name'] = $row['sku_name'];
				}
			}
			
			$select = $this->dealers_db->select();
			$select->from("b_sku", "*");
			
			if(1 == $id_type)
			{
				$select->where("product_id = ?", $id);
			}else{
				$select->where("parent_sku_id = ?", $id);
			}
			
			$data = $this->dealers_db->fetchAll($select);
			
			$parent_sku_id = array();
			
			if(!empty($data))
			{
				foreach($data as $d)
				{
					$parent_sku_id[] = $d['sku_id'];
					
					$result['data'][$d['sku_id']] = array(
						"parent_sku_id" => $d['parent_sku_id'],
						"sku_name" => $d['sku_name'],
						"unit_name" => $d['unit_name'],
						"unit_price" => $d['unit_price'],
						"status" => $d['status']
					);
				}
			}
			
			if(2 == $select_type)
			{
				$result['relation'] = array();
				$result['relation-map'] = array();
				
				while(!empty($parent_sku_id))
				{
					$select = $this->dealers_db->select();
					$select->from("b_sku", "*");
					$select->where("parent_sku_id IN (?)", $parent_sku_id);
					$select->where("status = ?", 1);
					$rows = $this->dealers_db->fetchAll($select);
					
					$parent_sku_id = array();
					
					if(!empty($rows))
					{
						foreach($rows as $row)
						{
							$parent_sku_id[] = $row['sku_id'];
							
							if(!isset($result['relation'][$row['parent_sku_id']]))
							{
								$result['relation'][$row['parent_sku_id']] = array();
							}
							
							$result['relation'][$row['parent_sku_id']][$row['sku_id']] = array(
								"sku_name" => $row['sku_name'],
								"unit_name" => $row['unit_name'],
								"unit_price" => $row['unit_price'],
								"status" => $row['status']
							);
						}
					}
				}
			}
		}
		
		return $result;
	}
	
	function GetSkuPath($sku_id)
	{
		$result = array(
			"path_arr" => array(),
			"path_name" => ""
		);
		
		$product_id = NULL;
		$first = 1;
		
		while($sku_id)
		{
			$select = $this->dealers_db->select();
			$select->from("b_sku", "*");
			$select->where("sku_id = ?", $sku_id);
			$row = $this->dealers_db->fetchRow($select);
			
			if($row)
			{
				$result['path_arr'][] = $row['sku_id'];
				
				if($first)
				{
					$result['path_name'] = $row['sku_name'];
					$first = 0;
				}else{
					$result['path_name'] = $row['sku_name']." > ".$result['path_name'];
				}
				
				$sku_id = $row['parent_sku_id'];
				$product_id = $row['product_id'];
			}
		}
		
		if($product_id)
		{
			$select = $this->dealers_db->select();
			$select->from("b_dict_product", "product_name");
			$select->where("product_id = ?", $product_id);
			$row = $this->dealers_db->fetchRow($select);
			
			if($row)
			{
				$result['path_name'] = $row['product_name']." > ".$result['path_name'];
			}
		}
		
		return $result;
	}
	
	function GetSkuRow($sku_id)
	{
		$select = $this->dealers_db->select();
		$select->from("b_sku as s", array("sku_id", "product_id", "parent_sku_id", "sku_name", "unit_name", "unit_price", "status"));
		$select->joinLeft("b_dict_product as p", "p.product_id=s.product_id", array("product_name", "status as product_status"));
		$select->where("s.sku_id = ?", $sku_id);
		$row = $this->dealers_db->fetchRow($select);
		
		if(!$row['product_id'])
		{
			$product_id =0;
			$sku_id = $row['sku_id'];
			
			while(!$product_id && $sku_id)
			{
				$select = $this->dealers_db->select();
				$select->from("b_sku", "*");
				$select->where("sku_id = ?", $sku_id);
				$s = $this->dealers_db->fetchRow($select);

				if($s)
				{
					if($s['product_id'])
					{
						$product_id = $s['product_id'];
					}else{
						$sku_id = $s['parent_sku_id'];
					}
				}
			}
			
			$select = $this->dealers_db->select();
			$select->from("b_dict_product", array("product_id", "product_name", "status as product_status"));
			$select->where("product_id = ?", $product_id);
			$product_row = $this->dealers_db->fetchRow($select);
			
			$row['product_id'] = $product_row['product_id'];
			$row['product_name'] = $product_row['product_name'];
			$row['product_status'] = $product_row['product_status'];
		}
		
		return $row;
	}
	
	function DuplicateRow($original_row, $target_parent_id)
	{
		$result = 0;
		
		if(!empty($original_row && $target_parent_id))
		{
			$set = array();
			
			$set['parent_sku_id'] = $target_parent_id;
			
			if($original_row['sku_name'])
			{
				$set['sku_name'] = $original_row['sku_name'];
			}
			
			if($original_row['unit_name'])
			{
				$set['unit_name'] = $original_row['unit_name'];
			}
			
			if($original_row['unit_price'])
			{
				$set['unit_price'] = $original_row['unit_price'];
			}
			
			$set['status'] = 1;
			
			$this->dealers_db->insert("b_sku", $set);
			
			$select = $this->dealers_db->select();
			$select->from("b_sku", array("sku_id"));
			$select->order("sku_id DESC");
			$row = $this->dealers_db->fetchRow($select);
			
			$result = $row['sku_id'];
		}
		
		return $result;
	}
}